
$(document).ready(function () {
    addjumpPagesEvent();
    showSumCount();
    goodsInfo=changeCartInfo();
    saveGoodsInfo( goodsInfo);
    showCartList(goodsInfo);
    showSumPrice(goodsInfo);
    addButtonEvent();
})
function addjumpPagesEvent() {
    $("#label").click(function () {
        location.assign("../html/productList.html");
    });
    $("#home").click(function () {
        location.assign("../html/home.html");
    });
    $("#productList").click(function () {
        location.assign("../html/productList.html");
    })
    $(".payMoney").click(function(){
        location.assign("../html/settlementList.html");
    })
}
function showCartList(goodsInfo){
    $("#templet").tmpl(goodsInfo).appendTo('#cartInfo');
}
function getGoodsInfo() {
    return JSON.parse(localStorage.getItem("goodsInfo"));
}
function saveGoodsInfo(goodsInfo) {
    localStorage.setItem("goodsInfo", JSON.stringify(goodsInfo));
}
function changeCartInfo() {
    var goodsInfo=getGoodsInfo();
    goodsInfo=computePromotionCount(goodsInfo);
    goodsInfo=computeSubTotal(goodsInfo);
    return goodsInfo;
}
function computePromotionCount(goodsInfo) {
    $.ajaxSettings.async=false;
    $.getJSON("../js/promotionName.json",function(info){
       _.each(goodsInfo, function (item) {
            item.promotionCount = (info.indexOf(item.name) > -1) ? parseInt(item.count / 3) : 0;
        })
    })
    return goodsInfo;
}
function computeSubTotal(goodsInfo) {
    return _.each(goodsInfo, function(item){
        if(item.promotionCount > 0){
            item.subTotal=(item.price * (item.count - item.promotionCount)) + '元（原价:' + item.count * item.price + '元）';
        }else{
            item.subTotal= (item.count * item.price + '元');
        }
    })
}
function showSubTotal(shoppingName,currentRow, goodsInfo) {
    _.each(goodsInfo, function (item) {
        if (shoppingName== item.name) {
            currentRow.find('.subTotal').html(item.subTotal);
        }
    })
}
function computeSumCount(changeCount){
    var cartCount = JSON.parse(localStorage.getItem("cartCount"));
     cartCount += changeCount;
    localStorage.setItem("cartCount", JSON.stringify(cartCount));
}
function showSumCount(){
    var cartCount = JSON.parse(localStorage.getItem("cartCount"))||0;
    if(cartCount==0){
        location.assign("../html/productList.html")
    }else{
        $("#cartCount").html(cartCount);
    }
}
function showSumPrice(goodsInfo) {
    var sumPrice = 0;
    _.each(goodsInfo, function (item) {
        sumPrice += parseFloat(item.subTotal);
    })
    $("#sumPrice").html(sumPrice);
}
function updataCount(shoppingName,changeCount) {
    var goodsInfo=getGoodsInfo();
    var result=[];
    _.each(goodsInfo,function(item){
        if(shoppingName == item.name){
            item.count +=changeCount;
        }
        if(item.count!=0 ){
            result.push(item)
        }
    });
    return result;
}
function updataNameArray(goodsInfo){
    var result=[];
    _.each(goodsInfo,function (item) {
        result.push(item.name);
    });
   localStorage.setItem("goodsName", JSON.stringify(result));
}
function showCount(goodsInfo,shoppingName,currentRow){
    var goodsName=JSON.parse(localStorage.getItem("goodsName"));
    _.each(goodsInfo,function(item){
        if(goodsName.indexOf(shoppingName)==-1) {
            currentRow.html("");
        }
        if(shoppingName==item.name){
            currentRow.find(".itemCount").html(item.count);
        }
    })
}
function updataLocalStory(shoppingName,changeCount){
    var goodsInfo=updataCount(shoppingName,changeCount);
    goodsInfo = computePromotionCount(goodsInfo);
    goodsInfo = computeSubTotal(goodsInfo);
    updataNameArray(goodsInfo);
    saveGoodsInfo(goodsInfo);
    return goodsInfo
}
function addButtonEvent(){
    $("td button").click(function(){
        var currentRow= $(this).parents('tr');
        var shoppingName=currentRow.find(".name").text();
        var changeCount = this.className.match('buttonLess')? -1 : 1;
        computeSumCount(changeCount);
        showSumCount();
        goodsInfo=updataLocalStory(shoppingName,changeCount);
        showSumPrice(goodsInfo);
        showSubTotal(shoppingName,currentRow,goodsInfo);
        showCount(goodsInfo,shoppingName,currentRow);
    });
}
